<?php
/**
 * 控制器基类
 * @author Administrator
 *
 */
class LoginController extends Zend_Controller_Action
{
	/**
	 * 系统配置
	 * @var Zend_Session_Namespace
	 */
	protected $_appconf;
	
	public $_session_namespace = Application_Model_Login::LOGIN_SESSION_KEY;
	
	protected $_user;
	protected $_user_id;
	protected $_user_name;

    public function init()
    {
    	//读取系统配置
		$this->_appconf = new Zend_Session_Namespace(Bootstrap::APP_CONF_SESSION_KEY);
		//设置页面静态元素路径
		//$this->view->pagePath = $this->_appconf->pageStaticPath;
		$this->view->pagePath = $this->_appconf->pageStaticPath ?$this->_appconf->pageStaticPath : "http://" . $_SERVER["HTTP_HOST"];
		$this->view->pageTitle = $this->_appconf->appName;
		//配置助手
    	Zend_Controller_Action_HelperBroker::addPath( APPLICATION_PATH . '/controllers/helpers', 'Application_Controller_Helper');
    	//配置布局
    	$this->_helper->layout->setLayout('layout');
    	//session
    	$this->_user = new Zend_Session_Namespace($this->_session_namespace);
    	
    	$url = "/".$this->getRequest()->getControllerName() ."/". $this->getRequest()->getActionName();
    	if(!Application_Model_Firewall::isAllow($url)){
	    	if(!($this->getRequest()->getControllerName() == "login")){    		
	    		if(empty($this->_user->id)){
	    			Zend_Session::destroy();
	    			$this->_redirect('login');
	    		}
	    	}
    	}
    	//配置user登录信息
		$this->_user_id = $this->_user->id;
		$this->_user_name = $this->_user->showname;
		
		$this->view->userid = $this->_user_id;
		$this->view->username = $this->_user_name;
    }

    public function indexAction()
    {
        // action body
        if(!empty($this->_user->id)){
        	$this->_redirect("index");
        }
        $this->_helper->layout()->disableLayout();
    }

    public function loginAction(){
    	$username = $this->getRequest()->getParam("username");
    	$password = $this->getRequest()->getParam("password");
    	$this->_user->id = 1;
		$this->_user->name = "test";
		$this->_user->showname = "张三";
    	$this->_redirect('index');
    }


    public function logoutAction(){
    	Zend_Session::destroy();
    	$this->_redirect('login');
    }
    
	public function listAction()
    {
    	//获取模块和模块控制器的路径
    	$modulesArr = $this->getFrontController()->getDispatcher()->getControllerDirectory();
 		//die(print_r($modulesArr));
    	//遍历所有控制器
    	foreach ($modulesArr as $mName=>$mDir){
    		echo "\n\n\n\n==========================================================================\n";
    		echo "这里是模块名",$mName;
    		echo "\n==========================================================================";
    		if ($handle = opendir($mDir)) {			
			    /* 这是正确地遍历目录方法 */
    			$modleM = new Application_Model_DbTable_Resource();
			    while (false !== ($file = readdir($handle))) {
			    	$fileDir = $mDir.'/'.$file;
			    	if(is_file($fileDir)){
						try {
				    		//组装控制器类名
				    		$classNmae = ucfirst((($mName == 'default')?'':$mName .'_'). (str_replace('.php','',$file)));
				    		$tempArr = array(
				    			"PortController",
				    			"ProductController",
				    			"PurchaseController",
				    			"QuoteController",
				    			"UserController"
				    		);
				    		
				    		if(!in_array($classNmae, $tempArr)){
				    			continue;
				    		}
				    		if(!class_exists($classNmae)) {
				    			//Zend_Loader::loadClass($classNmae,$fileDir);
				    			Zend_Loader::loadFile($file,$mDir,True);
				    		}
				    		echo "\n\n=====================================\n";
				    		echo "这里是控制器名",$mName,":",$classNmae;
				    		echo "\n=====================================\n";
				    		
				    		//反射文档
				    		$rc = new ReflectionClass($classNmae);
				    		$comment = $rc->getDocComment();
				    		$ctrlComment = "";
				    		if(preg_match_all('/\*\*\n \*(.+)\n/', $comment, $matches)){
				    			$ctrlComment = $matches[1][0];
				    		}
				    		echo $ctrlComment,"\n";
				    		
				    		$data = array(
				    			"title" => $ctrlComment,
				    			"deep" => 0
				    		);
				    		
				    		$ctrlId = $modleM->insert($data);
				    		
				    		echo "\n";
				    		foreach ($rc->getMethods() as $property) {
				    			$m = $property->getName();
				    			if(substr($m,-6,6) == 'Action'){
				    				$comment = $property->getDocComment();
				    				//echo $comment,"\n";
				    				echo "\n",$mName,"/",strtolower(str_replace("Controller", "", $classNmae)),"/",str_replace("Action", "", $m) ,"\n";
				    				
				    				$key = $mName."/".strtolower(str_replace("Controller", "", $classNmae))."/".str_replace("Action", "", $m);
					    			$methodComment = "";
						    		if(preg_match_all('/\*\*\n +\*(.+)\n/', $comment, $matches)){
						    			$methodComment = $matches[1][0];
						    		}
						    		echo $methodComment,"\n";
						    		if(in_array($m, array(
						    			"loginAction","logoutAction","listAction"
						    		))){
						    			continue;
						    		}
						    		
						    		$data = array(
						    			"key" => $key,
						    			"pid" => $ctrlId,
						    			"deep"=> 1,
						    			"title"=>$methodComment
						    		);
						    		$modleM->insert($data);
				    			}
				    		}
				    		
						}catch (Exception $e ){
							echo $e->getMessage();
						}
			    	}
			    }
			    closedir($handle);
			}
    	}
    	die();
    }
}

